package com.ysh.sevlet;

import com.ysh.domain.User;
import com.ysh.service.UserService;
import com.ysh.service.impl.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/*
    User逻辑控制层
 */
@WebServlet("/user")
public class UserServlet extends HttpServlet {
    // 创建service对象
    private UserService userService = new UserServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取路径中指定的方法名称
        String method = req.getParameter("method");
        // 判断，并执行相应方法
        if ("findAll".equals(method)){
            findAll(req,resp);
        }
        if ("add".equals(method)){
            add(req,resp);
        }
        if ("del".equals(method)){
            delete(req,resp);
        }
        if ("findById".equals(method)){
            findById(req,resp);
        }
        if ("updateByUser".equals(method)){
            updateByUser(req,resp);
        }
    }

    protected void updateByUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取页面数据
        Map<String, String[]> map = req.getParameterMap();
        User user = new User();
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        userService.update(user);
        // 跳回查询所有页
        resp.sendRedirect(req.getContextPath()+"/user?method=findAll");

    }

    // 根据id查询
    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1.获取用户id
        String uid = req.getParameter("uid");
        // 2.调用service层执行删除
        User user = userService.findById(uid);
        // 3.查询到结果,存储
        req.setAttribute("user",user);
        // 4.查询完成，跳回页面
        req.getRequestDispatcher("/pages/update.jsp").forward(req,resp);
    }

    // 删除功能
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1.获取用户id
        String uid = req.getParameter("uid");
        // 2.调用service层执行删除
        userService.deleteByIds(uid);
        // 3.删除完成，跳回页面
        req.getRequestDispatcher("/user?method=findAll").forward(req,resp);
    }

    // 添加功能
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取页面数据
        Map<String, String[]> map = req.getParameterMap();
        // 导入外部jar包，将数据封装到一个User对象中
        // BeanUtils
        User user = new User();
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        // System.out.println(user);
        int result = userService.addUser(user);
        // 添加成功，跳转到遍历页面
        if (result!=0) {
            req.getRequestDispatcher("/user?method=findAll").forward(req, resp);
        }else{
            resp.getWriter().write("添加失败！");
        }
    }

    // 查询所有用户方法
    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<User> list = userService.findAll();
        // 存入request作用域
        req.setAttribute("users",list);
        // 跳转
        req.getRequestDispatcher("/pages/list.jsp").forward(req,resp);
    }
}
